package com.ruoyi.sxt.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.SxtCaidan;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;

import com.ruoyi.sxt.mapper.SxtCaidanMapper;
import com.ruoyi.sxt.mapper.SxtJueseCaidanMapper;
import com.ruoyi.sxt.service.ISxtCaidanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 菜单权限Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-26
 */
@Service
public class SxtCaidanServiceImpl implements ISxtCaidanService
{
    public static final String PREMISSION_STRING = "perms[\"{0}\"]";
    @Autowired
    private SxtCaidanMapper sxtCaidanMapper;

    @Autowired
    private SxtJueseCaidanMapper sxtJueseCaidanMapper;

    /**
     * 查询菜单权限
     * 
     * @param menuId 菜单权限主键
     * @return 菜单权限
     */
    @Override
    public SxtCaidan selectSxtCaidanByMenuId(Long menuId)
    {
        return sxtCaidanMapper.selectSxtCaidanByMenuId(menuId);
    }

    /**
     * 查询菜单权限列表
     * 
     * @param sxtCaidan 菜单权限
     * @return 菜单权限
     */
    @Override
    public List<SxtCaidan> selectSxtCaidanList(SxtCaidan sxtCaidan)
    {
        return sxtCaidanMapper.selectSxtCaidanList(sxtCaidan);
    }

    @Override
    public List<TreeSelect> buildCaidanTreeSelect(List<SxtCaidan> caidans) {
        List<SxtCaidan> menuTrees = buildCaidanTree(caidans);
        return menuTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    @Override
    public List<Long> selectJueseCaidanListById(Long roleId) {
        return sxtJueseCaidanMapper.selectJueseCaidanByCaidanId(roleId);
    }

    public List<SxtCaidan> buildCaidanTree(List<SxtCaidan> caidans)
    {
        List<SxtCaidan> returnList = new ArrayList<SxtCaidan>();
        List<Long> tempList = caidans.stream().map(SxtCaidan::getMenuId).collect(Collectors.toList());
        for (Iterator<SxtCaidan> iterator = caidans.iterator(); iterator.hasNext();)
        {
            SxtCaidan caidan = (SxtCaidan) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(caidan.getParentId()))
            {
                recursionFn(caidans, caidan);
                returnList.add(caidan);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = caidans;
        }
        return returnList;
    }

    private void recursionFn(List<SxtCaidan> list, SxtCaidan t)
    {
        // 得到子节点列表
        List<SxtCaidan> childList = getChildList(list, t);
        t.setChildren(childList);
        for (SxtCaidan tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    private List<SxtCaidan> getChildList(List<SxtCaidan> list, SxtCaidan t)
    {
        List<SxtCaidan> tlist = new ArrayList<SxtCaidan>();
        Iterator<SxtCaidan> it = list.iterator();
        while (it.hasNext())
        {
            SxtCaidan n = (SxtCaidan) it.next();
            if (n.getParentId().longValue() == t.getMenuId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    private boolean hasChild(List<SxtCaidan> list, SxtCaidan t)
    {
        return getChildList(list, t).size() > 0;
    }

    /**
     * 新增菜单权限
     * 
     * @param sxtCaidan 菜单权限
     * @return 结果
     */
    @Override
    public int insertSxtCaidan(SxtCaidan sxtCaidan)
    {
        sxtCaidan.setCreateTime(DateUtils.getNowDate());
        return sxtCaidanMapper.insertSxtCaidan(sxtCaidan);
    }

    /**
     * 修改菜单权限
     * 
     * @param sxtCaidan 菜单权限
     * @return 结果
     */
    @Override
    public int updateSxtCaidan(SxtCaidan sxtCaidan)
    {
        sxtCaidan.setUpdateTime(DateUtils.getNowDate());
        return sxtCaidanMapper.updateSxtCaidan(sxtCaidan);
    }

    /**
     * 批量删除菜单权限
     * 
     * @param menuIds 需要删除的菜单权限主键
     * @return 结果
     */
    @Override
    public int deleteSxtCaidanByMenuIds(Long[] menuIds)
    {
        return sxtCaidanMapper.deleteSxtCaidanByMenuIds(menuIds);
    }

    /**
     * 删除菜单权限信息
     * 
     * @param menuId 菜单权限主键
     * @return 结果
     */
    @Override
    public int deleteSxtCaidanByMenuId(Long menuId)
    {
        return sxtCaidanMapper.deleteSxtCaidanByMenuId(menuId);
    }
}
